/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import com.prefabware.commons.Namespace;
import com.prefabware.commons.NamespaceImpl;
import com.prefabware.commons.file.Path;
import com.prefabware.commons.file.RelativePath;

public class JPackage {

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((namespace == null) ? 0 : namespace.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		JPackage other = (JPackage) obj;
		if (namespace == null) {
			if (other.namespace != null)
				return false;
		} else if (!namespace.equals(other.namespace))
			return false;
		return true;
	}

	private Namespace namespace;

	public JPackage(String uri) {
		super();
		namespace = createNamespace(uri);
	}

	/**
	 * appends the package with the given name to tje existing
	 * @param string
	 * @return the resulting package
	 */
	public JPackage append(String string) {
		return new JPackage(namespace.append(createNamespace(string)).packageStyle());
	}

	private Namespace createNamespace(String string) {
		return new NamespaceImpl(string,NamespaceImpl.SEPARATOR_PACKAGE);
	}

	public String getName() {
		return namespace.packageStyle();
	}

	protected Namespace getNamespace() {
		return namespace;
	}

	@Override
	public String toString() {
		return "JPackage [namespace=" + namespace + "]";
	}

	public Path getPath() {
		return new RelativePath(this.namespace.pathStyle());
	}
}
